/*
 * UpdateHistoryDialog.java
 *
 * Created on __DATE__, __TIME__
 */

package ui;

import java.awt.Dimension;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JOptionPane;

import bean.ConnectionBean;

/**
 *
 * @author  __USER__
 */
public class UpdateHistoryDialog extends javax.swing.JDialog
{
	
	/** Creates new form UpdateHistoryDialog */
	public UpdateHistoryDialog(java.awt.Frame parent, boolean modal, String refId, EditHistoryDialog editHistoryDialog)
	{
		super(parent, modal);
		initComponents();
		setPreferredSize(new Dimension(700, 500));
		fillUIFields(refId);
		this.editHistoryDialog = editHistoryDialog;
	}
	
	private void fillUIFields(String refId)
	{
		try
		{
			Connection con = ConnectionBean.getConnection();
			PreparedStatement psm = con.prepareStatement("select * from dignosis where reference_id = ?");
			psm.setString(1, refId);
			ResultSet rs = psm.executeQuery();
			if (rs.next())
			{
				idName.setText(rs.getString("patient_name").trim());
				idPhone.setText(rs.getString("phone_no").trim());
				idRefId.setText(rs.getString("reference_id").trim());
				idDate.setText(rs.getString("dignosis_date").trim());
				idDisease.setText(rs.getString("disease").trim());
				idDignosis.setText(rs.getString("dignosis").trim());
				idPrescription.setText(rs.getString("prescription").trim());
				
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{
		
		jLabel1 = new javax.swing.JLabel();
		idName = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		idPhone = new javax.swing.JTextField();
		jLabel3 = new javax.swing.JLabel();
		idRefId = new javax.swing.JTextField();
		jLabel4 = new javax.swing.JLabel();
		idDate = new javax.swing.JTextField();
		jLabel5 = new javax.swing.JLabel();
		idDisease = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		jLabel7 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		idDignosis = new javax.swing.JTextArea();
		jScrollPane2 = new javax.swing.JScrollPane();
		idPrescription = new javax.swing.JTextArea();
		idUpdate = new javax.swing.JButton();
		
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		
		jLabel1.setText("Name");
		
		idName.setEditable(false);
		
		jLabel2.setText("Phone No.");
		
		idPhone.setEditable(false);
		
		jLabel3.setText("Ref. ID");
		
		idRefId.setEditable(false);
		
		jLabel4.setText("Date");
		
		idDate.setEditable(false);
		
		jLabel5.setText("Disease");
		
		jLabel6.setText("Dignosis");
		
		jLabel7.setText("Prescription");
		
		idDignosis.setColumns(20);
		idDignosis.setLineWrap(true);
		idDignosis.setRows(5);
		idDignosis.setWrapStyleWord(true);
		jScrollPane1.setViewportView(idDignosis);
		
		idPrescription.setColumns(20);
		idPrescription.setLineWrap(true);
		idPrescription.setRows(5);
		idPrescription.setWrapStyleWord(true);
		jScrollPane2.setViewportView(idPrescription);
		
		idUpdate.setText("Update");
		idUpdate.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				idUpdateActionPerformed(evt);
			}
		});
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addGroup(
								layout.createSequentialGroup().addContainerGap().addComponent(idUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 157,
										javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(
								javax.swing.GroupLayout.Alignment.LEADING,
								layout.createSequentialGroup().addContainerGap().addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addComponent(jScrollPane1,
												javax.swing.GroupLayout.Alignment.LEADING).addGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												layout.createSequentialGroup().addGroup(
														layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addComponent(
																jLabel6, javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE).addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																layout.createSequentialGroup().addGroup(
																		layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addComponent(
																				jLabel4, javax.swing.GroupLayout.Alignment.LEADING,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(
																				jLabel1, javax.swing.GroupLayout.Alignment.LEADING,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
																		layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(
																				idDate).addComponent(idName, javax.swing.GroupLayout.DEFAULT_SIZE,
																				151, Short.MAX_VALUE)))).addPreferredGap(
														javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
														layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(
																jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE).addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))).addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addGroup(
												layout.createSequentialGroup().addGroup(
														layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(
																jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE).addComponent(idDisease).addComponent(idPhone,
																javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)).addPreferredGap(
														javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel3).addPreferredGap(
														javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(idRefId,
														javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)).addComponent(
												jScrollPane2)))).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1).addComponent(idName,
								javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
								jLabel2).addComponent(idPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel3).addComponent(idRefId,
								javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel4).addComponent(idDate,
								javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
								jLabel5).addComponent(idDisease, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel6).addComponent(jLabel7)).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane2,
								javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE).addComponent(jScrollPane1,
								javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(idUpdate).addGap(18, 18, 18)));
		
		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	private void idUpdateActionPerformed(java.awt.event.ActionEvent evt)
	{
		Connection con = ConnectionBean.getConnection();
		try
		{
			PreparedStatement psm =
					con.prepareStatement("update dignosis set disease = ?, dignosis = ?, prescription = ? where reference_id = ? and patient_name = ? and phone_no = ?");
			psm.setString(1, idDisease.getText().trim());
			psm.setString(2, idDignosis.getText().trim());
			psm.setString(3, idPrescription.getText().trim());
			psm.setString(4, idRefId.getText().trim());
			psm.setString(5, idName.getText().trim());
			psm.setString(6, idPhone.getText().trim());
			int i = psm.executeUpdate();
			if (i == 1) JOptionPane.showMessageDialog(this, "Dignosis Updated Successfully");
			else JOptionPane.showMessageDialog(this, "There is a problem in Updation. Contact Rituraj " + i);
			this.dispose();
			editHistoryDialog.fillTableDate();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}
	
	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JTextField idDate;
	private javax.swing.JTextArea idDignosis;
	private javax.swing.JTextField idDisease;
	private javax.swing.JTextField idName;
	private javax.swing.JTextField idPhone;
	private javax.swing.JTextArea idPrescription;
	protected javax.swing.JTextField idRefId;
	private javax.swing.JButton idUpdate;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	// End of variables declaration//GEN-END:variables
	private EditHistoryDialog editHistoryDialog;
	
}
